package com.tutor.lzm;

import android.content.Context;
import android.nfc.Tag;

import com.tutor.lzm.app.Constant;
import com.tutor.lzm.bean.SubjectBean;
import com.tutor.lzm.bean.UserInfo;
import com.tutor.lzm.http.UserInfoResponse;
import com.tutor.lzm.utils.SPKey;
import com.tutor.lzm.utils.SharedPreferenceHelper;
import com.tutor.syslib.action.SysSession;
import com.tutor.syslib.pulltorefresh.PullToRefreshBase;
import com.tutor.syslib.util.DateTimeHelper;
import com.tutor.syslib.util.TextUtil;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * Created by zzgpc on 2017-9-6.
 */

public class AppSession extends SysSession {
    private static final String TAG = "AppSession";

    public static String SPTAG;
    private static Context mContext;
    private static AppSession mInstance = null;

    public List<SubjectBean> getSubjectList() {
        return subjectList;
    }

    public void setSubjectList(List<SubjectBean> subjectList) {
        this.subjectList = subjectList;
    }

    /**
     * 所有的学科
     */
    private List<SubjectBean> subjectList = new ArrayList<>();
    /**
     * 用户id
     */
    private String userId;

    public UserInfo getUserInfo() {
//        if (userInfo == null) {
//            userInfo = SharedPreferenceHelper.getObject(TAG, SPKey.userInfo, UserInfo.class);
//        }
//        return userInfo;
        return userInfo = SharedPreferenceHelper.getObject(SPTAG, SPKey.userInfo, UserInfo.class);
    }

    public void setUserInfo(UserInfo userInfo) {
        this.userInfo = userInfo;
        AppSession.saveAppSessionData(SPKey.userInfo, userInfo);
    }

    private UserInfo userInfo;

    public boolean isTeacher() {
        isTeacher = AppSession.getSPDataObject("isTeacher", Boolean.class);
        return isTeacher;
    }

    public void setIsTeacher(boolean teacher) {
        AppSession.saveAppSessionData("isTeacher", teacher);
        isTeacher = teacher;
    }

    /**
     * 是否是老师
     */
    private boolean isTeacher = false;

    public AppSession(Context context) {
        mContext = context;
        mInstance = this;

//        mUnLoginActivityList.add("com.easygroup.ngaripatient.publicmodule.selectdoctor.SelectCityAreaActivity");
    }

    public static AppSession getInstance() {
        return mInstance;
    }

    public String getUserId() {
        if (TextUtil.isNull(userId)) {
            userId = AppSession.getAppSPData("userId");
        }

        return userId;
    }

    public void setUserId(String userId) {
        saveAppSessionData("userId", userId);
        this.userId = userId;
        SPTAG = TAG + "_" + userId;
    }

    /**
     * 保存一些初始化数据
     *
     * @param key
     * @param object
     */
    public static void saveAppSessionData(String key, Object object) {
        SharedPreferenceHelper.save(SPTAG, key, object);
    }

    /**
     * 获取保存在sp中的数据
     *
     * @param value
     * @return
     */
    public static String getAppSPData(String value) {
        return SharedPreferenceHelper.getObject(SPTAG, value, String.class);
    }

    /**
     * 保存一些初始化数据
     *
     * @param key
     * @param object
     */
    public static void saveAppSessionDataNoId(String key, Object object) {
        SharedPreferenceHelper.save(TAG, key, object);
    }

    /**
     * 获取保存在sp中的数据
     *
     * @param value
     * @return
     */
    public static String getAppSPDataNoId(String value) {
        return SharedPreferenceHelper.getObject(TAG, value, String.class);
    }

    public static <T> T getSPDataObject(String key, Class<T> clazz) {
        return SharedPreferenceHelper.getObject(SPTAG, key, clazz);
    }

    /**
     * 下拉刷新控件时间格式
     */
    public static final String FORMART_REFRESH_TIME = "yyyy-MM-dd HH:mm";

    /**
     * 设置下拉刷新的时间
     *
     * @param pull_refresh_list 下拉刷新控件
     */
    public static void setPullFormartRefreshTime(PullToRefreshBase pull_refresh_list) {
        long currentTime = System.currentTimeMillis();
        pull_refresh_list.setLastUpdatedLabel(DateTimeHelper.formatDate(new Date(currentTime), AppSession.FORMART_REFRESH_TIME));

    }
}
